from .mail import send_email

class warn():

    cpu_times = 0
    mem_times = 0
    disk_times = 0

    @classmethod
    def cpu_warn(cls,cpu_used,hostname,email):
        if float(cpu_used[:-1])>90:
            text = f"EMERGE! {hostname} cpu 使用率为{cpu_used} 大于90%"
            warn.cpu_times += 1

        elif float(cpu_used[:-1])>80:
            text = f"CRITICAL! {hostname} cpu 使用率为{cpu_used} 大于80%"
            warn.cpu_times += 1
            print(text,warn.cpu_times)

        elif float(cpu_used[:-1])>70:
            text = f"WARNING! {hostname} cpu 使用率为{cpu_used} 大于70%"
            warn.cpu_times += 1

        else:
            warn.cpu_times = 0

        if warn.cpu_times == 5:
            send_email(text,email)
            warn.cpu_times = 0

    @classmethod
    def mem_warn(cls,mem_used,hostname,email):
        if float(mem_used[:-1])>90:
            text = f"EMERGE! {hostname} 内存 使用率为{mem_used} 大于90%"
            warn.mem_times += 1

        elif float(mem_used[:-1])>80:
            text = f"CRITICAL! {hostname} 内存 使用率为{mem_used} 大于80%"
            warn.mem_times += 1
            print(text,warn.mem_times)

        elif float(mem_used[:-1])>70:
            text = f"WARNING! {hostname} 内存 使用率为{mem_used} 大于70%"
            warn.mem_times += 1

        else:
            warn.mem_times = 0

        if warn.mem_times == 5:
            send_email(text,email)
            warn.mem_times = 0

    @classmethod
    def disk_warn(cls,disk_used,hostname,email):
        if float(disk_used[:-1])>90:
            text = f"EMERGE! {hostname} 磁盘 使用率为{disk_used} 大于90%"
            warn.disk_times += 1

        elif float(disk_used[:-1])>80:
            text = f"CRITICAL! {hostname} 磁盘 使用率为{disk_used} 大于80%"
            warn.disk_times += 1
            print(text,warn.disk_times)

        elif float(disk_used[:-1])>70:
            text = f"WARNING! {hostname} 磁盘 使用率为{disk_used} 大于70%"
            warn.disk_times += 1

        else:
            warn.disk_times = 0

        if warn.disk_times == 5:
            send_email(text,email)
            warn.disk_times = 0





